package com.ssm.studen.controller;

import com.ssm.studen.model.Teacher;
import com.ssm.studen.service.TeacherService;
import com.ssm.studen.tool.ResponseMessage;
import com.ssm.studen.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 教师表：
 *      对外提供数据接口服务
 */
@CrossOrigin
@Api(tags = "teacher")
@Controller
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    /**********************教师添加方法***************************/
    public ResponseMessage addTeacher(Teacher teacher){
        int isAdd = 0;
        //传入数据判断
        //1.教师对象不能为空
        if(teacher != null){
            //2.教师基本信息不能为空
            if(teacher.getTid()!=null){
                //3.添加教师数据
                isAdd = teacherService.addTeacher(teacher);
                if(isAdd == 1){
                    return Result.success("保存成功");
                }else if(isAdd == -1){
                    return Result.error("教师重复不允许添加！");
                }else{
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }


    /**********************教师删除方法***************************/
    @ApiOperation("教师删除方法")
    @RequestMapping(value = "/deleteTeacher",method = RequestMethod.GET)
    public ResponseMessage delTeacher(String tid){
        if(tid != null){
            int delInt = 0;
            delInt = teacherService.delTeacher(tid);
            if(delInt>0){
                return Result.success("删除成功");
            }else{
                return Result.error("删除失败");
            }
        }else{
            return Result.error("删除的主键不能为“0”，请重新输入！");
        }
    }


    /***********************教师更新方法**************************/
    @ApiOperation("教师更新方法")
    @RequestMapping(value = "/updataTeacher",method = RequestMethod.GET)
    public ResponseMessage updataTeacher(Teacher teacher){
        //传入的参数不能为空
        if(teacher != null){
            //参数当中主键不能为0
            if(teacher.getTnum() != 0){
                if(teacherService.updateTeacher(teacher) == 1){
                    if(teacher.getTid()!=null||teacher.getTstart()!=null||teacher.getTlessonscope()!=null){
                        teacherService.updateTeacher(teacher);
                    }else{
                        return Result.error("更新失败");
                    }
                }else{
                    return Result.error("更新失败");
                }
            }
        }
        return Result.error("传入参数不正确");
    }
}
